package com.ogawa.a7517.fragment;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.ogawa.a7517.R;

public class FactoryPswFragment extends BaseFragment implements TextWatcher, View.OnClickListener {
    private ImageView ivPsw1, ivPsw2, ivPsw3, ivPsw4;
    private EditText etPsw;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_factory_password;
    }

    @Override
    public void onStart() {
        super.onStart();
        setTitle(getString(R.string.input_psw));
    }

    @Override
    public void initView(View view) {
        ivPsw1 = view.findViewById(R.id.iv_psw_1);
        ivPsw2 = view.findViewById(R.id.iv_psw_2);
        ivPsw3 = view.findViewById(R.id.iv_psw_3);
        ivPsw4 = view.findViewById(R.id.iv_psw_4);
        etPsw = view.findViewById(R.id.et_psw);
        etPsw.addTextChangedListener(this);
        view.findViewById(R.id.tv_psw_0).setOnClickListener(this);
        view.findViewById(R.id.tv_psw_1).setOnClickListener(this);
        view.findViewById(R.id.tv_psw_2).setOnClickListener(this);
        view.findViewById(R.id.tv_psw_3).setOnClickListener(this);
        view.findViewById(R.id.tv_psw_4).setOnClickListener(this);
        view.findViewById(R.id.tv_psw_5).setOnClickListener(this);
        view.findViewById(R.id.tv_psw_6).setOnClickListener(this);
        view.findViewById(R.id.tv_psw_7).setOnClickListener(this);
        view.findViewById(R.id.tv_psw_8).setOnClickListener(this);
        view.findViewById(R.id.tv_psw_9).setOnClickListener(this);
        view.findViewById(R.id.iv_back).setOnClickListener(this);
        view.findViewById(R.id.iv_delete).setOnClickListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        String psw = etPsw.getText().toString();
        int length = psw.length();
        ivPsw1.setSelected(length > 0);
        ivPsw2.setSelected(length > 1);
        ivPsw3.setSelected(length > 2);
        ivPsw4.setSelected(length > 3);
        if (length == 4 && "0592".equals(psw)) {
            activity7517.popBackStack();
            activity7517.startToFragmentAddToStack(new FactoryModeFragment());
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_psw_0) {
            etPsw.setText(etPsw.getText().toString() + "0");
        } else if (id == R.id.tv_psw_1) {
            etPsw.setText(etPsw.getText().toString() + "1");
        } else if (id == R.id.tv_psw_2) {
            etPsw.setText(etPsw.getText().toString() + "2");
        } else if (id == R.id.tv_psw_3) {
            etPsw.setText(etPsw.getText().toString() + "3");
        } else if (id == R.id.tv_psw_4) {
            etPsw.setText(etPsw.getText().toString() + "4");
        } else if (id == R.id.tv_psw_5) {
            etPsw.setText(etPsw.getText().toString() + "5");
        } else if (id == R.id.tv_psw_6) {
            etPsw.setText(etPsw.getText().toString() + "6");
        } else if (id == R.id.tv_psw_7) {
            etPsw.setText(etPsw.getText().toString() + "7");
        } else if (id == R.id.tv_psw_8) {
            etPsw.setText(etPsw.getText().toString() + "8");
        } else if (id == R.id.tv_psw_9) {
            etPsw.setText(etPsw.getText().toString() + "9");
        } else if (id == R.id.iv_delete) {
            int length = etPsw.getText().length();
            switch (length) {
                case 0:
                    break;
                case 1:
                    etPsw.setText("");
                    break;
                default:
                    etPsw.setText(etPsw.getText().toString().substring(0, length - 1));
                    break;
            }
        } else if (id == R.id.iv_back) {
            activity7517.popBackStack();
        }
    }
}
